package com.atguigu.gulimail.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberResponseVo;
import com.atguigu.gulimail.auth.feign.MemberFeignService;
import com.atguigu.gulimail.auth.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;


@Controller
public class LoginController {
    final static int a=0;


    @Autowired
    RedisTemplate jsonRedisTemplate;

    @Autowired
    MemberFeignService memberFeignService;


    @GetMapping("/login.html")
    public String loginPage(HttpSession session){
        Object attribute = session.getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){
                //如果不为null则返回到首页  否则到登录页。
            return "redirect:http://gulimail.com";
        }else{
            return "login";
        }

    }


    @PostMapping("/login")
    public String login(LoginVo loginVo, RedirectAttributes redirectAttributes,HttpSession session){

        //这里应该查询会员系统里面当前vo是否有数据。如果有 就登录成功。  否则。失败。
        //远程
        R login = memberFeignService.login(loginVo);
        if (login.getCode()==0){
            MemberResponseVo data = login.getData(new TypeReference<MemberResponseVo>() {
            });
            session.setAttribute(AuthServerConstant.LOGIN_USER, data);
            return "redirect:http://gulimail.com";
        }else{
            //失败。则重定向到登录页 并回显数据。
            Map<String,String> errors=new HashMap<>();
            errors.put("errorMsg", login.getMsg());
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimail.com/login.html";
        }

    }


    public static void main(String[]args){
        int a=1;
        short s1=2;
        int m=s1;
        float f=2.2f;
        int n= (int) f;
      new   CopyOnWriteArrayList();
      new LinkedList<>();
      new HashMap<>();
        HashSet<String> strings = new HashSet<>();
        strings.add("asd");
        strings.add("123");
        strings.add("555");
        System.out.println(strings);
        new LinkedHashMap<>();
      new LinkedHashSet<>();
      new ConcurrentHashMap<>();
      new RuntimeException();
        Class<Thread.State> stateClass = Thread.State.class;
        new Thread();
        new ThreadLocal();

    }

}
